# 基类
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.select import Select
class Base():
    # 驱动
    def __init__(self,driver):
        self.driver=driver
    # 打开网址
    def open_url(self,url):
        self.driver.get(url)
    # 封装方法
    def find_element(self,loc):
        try:
            WebDriverWait(self.driver,20,0.5).until(lambda driver: driver.find_element(*loc).is_displayed())
            return self.driver.find_element(*loc)
        except Exception as e:
            print(e)
    # 封装点击
    def click(self,loc):
        self.find_element(loc).click()
    # 封装输入
    def send_keys(self,loc,info):
        self.find_element(loc).send_keys(info)
    # 封装文本
    def text(self,loc):
        return self.find_element(loc).text
    # 回车
    def Enter(self,loc):
        self.find_element(loc).send_keys(Keys.ENTER)
    # ctrl
    def ctrl_v(self,loc):
        self.find_element(loc).send_keys(Keys.CONTROL,'v')
    def ctrl_c(self,loc):
        self.find_element(loc).send_keys(Keys.CONTROL,'c')
    def ctrl_x(self,loc):
        self.find_element(loc).send_keys(Keys.CONTROL,'x')
    # 空格
    def space(self,loc):
        self.find_element(loc).send_keys(Keys.SPACE)
    # 切换窗口
    def handles(self):
        self.driver.switch_to.window(self.driver.window_handles[-1])
    #鼠标悬停
    def move(self,loc):
        ActionChains(self.driver).move_to_element(self.find_element(loc)).perform()
    # 双击
    def double(self,loc):
        ActionChains(self.driver).double_click(self.find_element(loc)).perform()
    # 三击
    def three(self,loc):
        ActionChains(self.driver).double_click(self.find_element(loc)).click().perform()
    # 拖拽
    def drop(self,loc):
        ActionChains(self.driver).drag_and_drop(self.find_element(loc),self.find_element(loc)).perform()
    # frame框架
    def frame(self,loc):
        self.driver.switch_to.frame(self.find_element(loc))
    # 退出frame框架
    def frame_un(self):
        self.driver.switch_to.default_content()
    # 滚动任意像素
    def scrollTo_ry(self,x):
        self.driver.execute_script(f'window.scrollTo(0,{x})')
    # 滚动到底部
    def scrollTo_db(self):
        self.driver.execute_script('window.scrollTo(0,document.body.scrollHeight)')
    # 滚动到顶部
    def scrollTo_top(self):
        self.driver.execute_script('window.scrollTo(0,0)')
    # 弹窗
    def accept(self):
        accept = self.driver.switch_to.alert
        accept.accept()
    def send_accept(self,info):
        accept = self.driver.switch_to.alert
        accept.send_keys(info)
        accept.accept()
    def send_dismiss(self,info):
        accept = self.driver.switch_to.alert
        accept.send_keys(info)
        accept.dismiss()
    # 下拉
    def select_index(self,loc,index):
        vtext = Select(self.find_element(loc))
        vtext.select_by_index(index)
    def select_value(self,loc,value):
        vtext = Select(self.find_element(loc))
        vtext.select_by_value(value)
    def select_text(self,loc,text):
        vtext = Select(self.find_element(loc))
        vtext.select_by_visible_text(text)